package com.ehualu.check.es;

import com.ehualu.check.utils.LoadProperties;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.ExecutionException;

/**
 * @Author: 吴敬超
 * @Date: 2021/3/24 10:05
 */
public class EsUpdateUtil {

    private static final Logger LOG = LoggerFactory.getLogger(EsUpdateUtil.class);


    static LoadProperties loadProc = LoadProperties.getInstance();

    public void updateIndex(String esid,
                            String esMD5name, String esMD5value,
                            String esSHA256name, String esSHA256value,
                            String extHashStatusname, String extHashStatus)
            throws IOException, ExecutionException, InterruptedException {


        LOG.info("修改的id：" + esid);

        Settings settings = Settings.builder()
                .put("cluster.name", loadProc.getValues("clusterName", "test"))
                .put("client.transport.sniff", false)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(
                        InetAddress.getByName(loadProc.getValues("esHost", "172.16.101.63")),
                        Integer.parseInt(loadProc.getValues("esPort", "9300"))));
        UpdateRequest updateRequest = new UpdateRequest();

        updateRequest.index(loadProc.getValues("esIndices", "data_center_platform_csh"))
                .type(loadProc.getValues("esType", "data_ext"))
                .id(esid)
                .doc(XContentFactory.jsonBuilder()
                        .startObject()
                        .field(esMD5name, esMD5value)
                        .field(esSHA256name, esSHA256value)
                        .field(extHashStatusname, extHashStatus)
                        .endObject()
                );


        LOG.info("执行修改");
        UpdateResponse response = client.update(updateRequest).get();
        //查询修改状态，返回ok表示成功
        LOG.info("查询修改状态，返回ok表示成功：" + response.status());

        client.close();
        LOG.info("ES client close");


    }


/*
    public static void main(String[] args) {


        EsUpdateUtil esUpdateUtil = new EsUpdateUtil();
        try {
            esUpdateUtil.updateIndex("2beb9158cd3a436f8acedea7bd9b9f22",
                    "md5Hash", "853bfdd9d89a99b2b54ad4e04111b88c",
                    "sha256Hash", "b11d7d33699ca4f60bc3b2b6858fd953e3de2b8522c943f4cda4b674316196a8",
                    "extHashStatus", "1");
        } catch (Exception e) {
            LOG.info("修改失败id：");
            e.printStackTrace();
        }

    }
*/
}
